package dao;

import hbt.HibernateUtil;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import model.Torneo;

public class TorneoDAO {

	private static TorneoDAO dao;
	private static SessionFactory sf = null;
	
	public static TorneoDAO getInstance(){
		if(dao == null){
			sf = HibernateUtil.getSessionFactory();
			dao = new TorneoDAO();
		} 
		return dao;
	}
	
	
	public boolean persist(Torneo torneo){
		try{
			Session session = sf.openSession();
			session.beginTransaction();	
			session.persist(torneo);
			session.flush();
			session.getTransaction().commit();
			session.close();
			
		    return true;
		}catch(Exception e){
			return false;
		}
	}
	public boolean update(Torneo torneo){
		
		try{	
			Session session = sf.openSession();
			Torneo t = (Torneo)session.get(Torneo.class, torneo.getId()); 
			System.out.println("torrrr " + torneo.getId());
			t.addCategoria(torneo.getCategoria('A'));
			session.update(t); 
			session.close();
			return true;
		}catch(Exception e){
			return false;
		}
	}
	public boolean delete(Torneo torneo){
		try{
			Session session = sf.openSession();
		    session.delete(torneo);
		    session.close();
		    return true;
		}catch(Exception e){
			return false;
		}
	}
	
	
	public Torneo obtenerTorneo(int idTorneo) {
		Session session = sf.openSession();
		Query cs = session.createQuery("from Torneo where id=:param").setInteger("param",idTorneo);
		Torneo t = (Torneo)cs.uniqueResult();
		System.out.println(t.getExtendedView().getCategorias().size());
		return t;
	}

}
